//函数定义
//没有定义函数返回值，ts自动推断
function fadd1(x: number, y: number) {
	return x + y;
}

//以下3种只定义的函数的接口，未定义函数实现
let fadd2: (x: number, y: number) => number;

type fadd3 = (x: number, y: number) => number;

interface fadd4 {
	(x: number, y: number): number;
}

function fadd5(x: number, y?: number) {
	return y ? x + y : x;
}
let aa: fadd3 = (x, y) => {
	return x + y;
};
fadd2 = function(x, y) {
	return 1;
};
let bb: fadd4 = function(x, y) {
	return 1;
};
console.log(fadd5(4));
console.log(fadd5(1, 2));

function fadd6(x: number, y = 0, z: number, q = 1) {
	return x + y + z + q;
}

console.log(fadd6(1, undefined, 3));

function fadd7(x: number, ...rest: number[]) {
	return x + rest.reduce((pre, cur) => pre + cur);
}
console.log(fadd7(1, 2, 3, 4));

//函数重载
function fadd8(...rest: number[]): number;
function fadd8(...rest: string[]): string;
function fadd8(...rest: any[]): any {
	let first = rest[0];
	if (typeof first === 'string') {
		return rest.join('');
	}
	if (typeof first === 'number') {
		return rest.reduce((pre, cur) => pre + cur);
	}
}
console.log(fadd8(1, 2, 3));
console.log('a' + 'b');
